/* GLVertexDataLocal.h
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */
#include <stdexcept>

#ifndef GLVERTEXDATALOCAL_H_
#define GLVERTEXDATALOCAL_H_

template <typename Tvect>
class GLVertexDataLocal : public GLVertexData<Tvect>
{
	float* data;

public:
	GLVertexDataLocal(int nVertices, int nComponents)
		: GLVertexData<Tvect>(nVertices, nComponents)
	{
		data = new float[nVertices * nComponents * sizeof(float)];
	}

	GLVertexDataLocal()
		: GLVertexData<Tvect>()
	{
		data = NULL;
	}

	~GLVertexDataLocal()
	{
		delete[] data;
	}

	void
	setGeometry(int v, int c)
	{
		GLVertexData<Tvect>::setGeometry(v, c);
		data = new float[v * c * sizeof(float)];
	}


	void
	init(std::initializer_list<Tvect> l)
	{
		typename std::initializer_list<Tvect>::const_iterator it, iend;
		int i;

		if (data == NULL)
			throw std::runtime_error("Initializing GLVertexDataLocal without "
				"defining geometry first.");

		int c = GLVertexData<Tvect>::nComponent;
		for (it = l.begin(), iend = l.end(), i=0; it != iend
			&& i < GLVertexData<Tvect>::nVertices; it++, i++)
		{
			for (int j = 0; j < c; j++)
				data[c*i+j] = (*it)[j];
		}
	}

	void
	init(Tvect* l)
	{
		if (data == NULL)
			throw std::runtime_error("Initializing GLVertexDataLocal without "
				"defining geometry first.");

		int c = GLVertexData<Tvect>::nComponent;
		for (int i = 0; i < GLVertexData<Tvect>::nVertices; i++)
		{
			for (int j = 0; j < c; j++)
				data[c*i+j] = l[i][j];
		}
	}

	const void*
	getAddress()
	{
		return (void*)data;
	}
};
#endif /* GLVERTEXDATALOCAL_H_ */
